package tezhongbing.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class HelloWorldHandler implements InvocationHandler {
	private Object target;
	
	private HelloWorldHandler(Object target) {
		this.target = target;
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		doBefore();
		Object result = method.invoke(target, args);
		doAfter();
		return result;
	}
	private void doAfter() {
		System.out.println("after method invoke");
	}
	private void doBefore() {
		System.out.println("before method invoke");
	}

	public static <T> T getInstance(T target){
		HelloWorldHandler handler = new HelloWorldHandler(target);
		return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
	}
	
}
